<?php
/*
	获取活动详情
	电商客商品列表页和商品详情页会调用本方法以获得商品详情
*/
defined('BY_JYA') or exit('error');
list($goods_id,$optionid,$mid)=$arguments;
$mid=$mid?:$_SESSION['uid'];
$member=$this->get('member',array('mid'=>$mid));
if($optionid){
	$goodsinfo=pdo_get('jy_weishop_goods_option',array('goodsid'=>$goods_id,'id'=>$optionid),array('costprice','price'));
}else{
	$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods_id),array('costprice','price'));
}
$goodsprice=$goodsinfo['price'];
$goodscostprice=$goodsinfo['costprice'];
$goods=$this->get('goods',array('goods_id'=>$goods_id),array('id','ignore','diyprice','commissions'));
if($goods['id'] && $goods['ignore']!=1){
	//计算分销等级价格
	$diyprice=$goods['diyprice']?json_decode($goods['diyprice'],true):array();
	foreach($diyprice as $gc){
			if($gc['level_id']==0){
				$price=$gc['price'];
			}
			if($gc['level_id']!=$member['level_id']){
				continue;
			}else{
				$price=$gc['price'];
				break;
			}
	}
	$goods['price']=$price;
	//计算佣金
	$goods_commission=$goods['commissions']?json_decode($goods['commissions'],true):array();
	foreach($goods_commission as $gc){
			if($gc['level_id']==0){
					$demogoods_commission_per=$gc['commission1'];
			}
			if($gc['level_id']!=$member['level_id']){
					continue;
			}else{
					$goods_commission_per=$gc['commission1'];
					break;
			}
	}
	$goods_commission_per=$goods_commission_per?:$demogoods_commission_per;	
	if(substr($goods_commission_per,-1)=='%'){
			//利润计算
			if($setting['commissionfeetype']==2){
				$goodsprice -=$goodscostprice;
			}
			$goods_commission_money=($goodsprice*substr($goods_commission_per,0,-1))/100;
	}else{
			$goods_commission_money=$goods_commission_per;
	}
	$goods['commission_money']=$goods_commission_money?price($goods_commission_money,true):0;
}elseif(!$goods['id']){
	if(!$member['level_id']){
		//获取默认等级佣金比例
		$level_commissions=$this->plugin_setting('default_level');
		$level_id=0;
		$commission_per=$level_commissions['commission1'];
	}else{
		$level=$this->get('level',array('id'=>$member['level_id'],'uniacid'=>$_SESSION['uniacid']));
		$level_commissions=json_decode($level['commissions'],true);
		$level_id=$level['id'];
		$commission_per=$level_commissions['commission1'];
	}
	if($setting['get_buyordernum']!=1){
		//利润计算
		if($this->setting['commissionfeetype']==2){
			$goodsprice -=$goodscostprice;
		}
		$order_commission_money=intval($goodsprice*$commission_per)/100;
	}else{//按笔分销
		$order_commission_money=$commission_per;
	}
	$goods['commission_money']=$order_commission_money>0?price($order_commission_money,true):0;
}else{
	$goods['commission_money']=0;
}
$goods['commission_title']=$setting['showgettitle']?:'预估赚';
$goods['commission_sysbutie']=price($setting['sysbutie'],true);
return $goods;